package com.xiumu.service.sys.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiumu.common.core.constants.XiuMuConst;
import com.xiumu.common.core.exception.user.AuthException;
import com.xiumu.common.core.page.PageQuery;
import com.xiumu.common.core.tree.XiuMuTreeUtil;
import com.xiumu.common.core.utils.AssertUtil;
import com.xiumu.common.core.utils.ValidatorUtils;
import com.xiumu.dao.sys.MenuDao;
import com.xiumu.pojo.sys.dto.MenuDTO;
import com.xiumu.pojo.sys.entity.Menu;
import com.xiumu.pojo.sys.query.MenuQuery;
import com.xiumu.service.sys.MenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 菜单表，菜单与权限是一对一的关系 Service 接口实现
 *
 * @author XiuMu
 * @Date 2023-01-18 10:07:33
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuDao, Menu> implements MenuService {

    @Override
    public Page<Menu> listPage(PageQuery<MenuQuery, Menu> pageQuery) {
        return this.baseMapper.selectPage(pageQuery.toPage(), pageQuery.getCondition());
    }

    @Override
    public List<Menu> listByMenu(MenuQuery menu) {
        return this.baseMapper.selectByMenu(menu);
    }

    @Override
    public List<Tree<Long>> listByAuthCodeList(List<String> authCodeList) {
        if (CollUtil.isEmpty(authCodeList)){
            return null;
        }
        List<Menu> menuList = list(new LambdaQueryWrapper<Menu>().in(Menu::getAuthCode, authCodeList));
        return XiuMuTreeUtil.buildTree(menuList, XiuMuConst.ZERO_LONG);
    }

    @Override
    public Menu getByAuthCode(String authCode) {
        return getOne(new LambdaQueryWrapper<Menu>().eq(Menu::getAuthCode, authCode));
    }

    @Override
    public List<Menu> listInAuthCodeList(List<String> authCodeList) {
        return list(new LambdaQueryWrapper<Menu>().in(Menu::getAuthCode, authCodeList));
    }

    @Override
    @Transactional
    public boolean saveOrUpdateByParentAuthIdAndAuthCodeAndMenuDTO(String parentAuthCode, String authCode, MenuDTO menuDTO) {
        AssertUtil.isNotNull(menuDTO, AuthException.NO_MENU_INFO);
        ValidatorUtils.validate(menuDTO);
        Menu menu = BeanUtil.toBean(menuDTO, Menu.class);
        menu.setAuthCode(authCode);
        // 判断该权限有没有菜单，有的话就是修改，没有就是新增
        Menu menuTemp = getByAuthCode(authCode);
        menu.setId(menuTemp != null ? menuTemp.getId() : null);
        // 父级权限编码为空则表示没有父级菜单
        menu.setParentId(XiuMuConst.ZERO_LONG);
        // 如果父级权限编码不为空，则将菜单的父级ID改为父级权限所对应的菜单ID
        if (parentAuthCode != null){
            Menu parentMenu = getByAuthCode(parentAuthCode);
            menu.setParentId(parentMenu.getId());
        }
        return saveOrUpdate(menu);
    }
}




